local L = AceLibrary("AceLocale-2.2"):new("WelcomeHome")

local options = {
	type='group',
	args = {
		msg = {
			type = 'text',
			name = L["Message"],
			desc = L["Sets the message to be displayed when you change zones."],
			usage = L["<your message>"],
			get = "GetMessage",
			set = "SetMessage",
		},
		showInChat = {
			type = 'toggle',
			name = 'Show in Chat',
			desc = 'Toggles the display of the message in the chat window',
			get = "IsShowInChat",
			set = "ToggleShowInChat",
		},
		showOnScreen = {
			type = 'toggle',
			name = 'Show on Screen',
			desc = 'Toggles the display of the message on the screen',
			get = "IsShowOnScreen",
			set = "ToggleShowOnScreen",
		},
	}
}

WelcomeHome = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceEvent-2.0", "AceDB-2.0", "CandyBar-2.0")
WelcomeHome:RegisterChatCommand(L["Slash-Commands"], options)
WelcomeHome:RegisterDB("WelcomeHomeDB", "WelcomeHomeDBPC")
WelcomeHome:RegisterDefaults('profile', {
	message = L["Zone Changed!"],
	showInChat = false,
	showOnScreen = true,
	frames = {
		anchorFrameLoc = {
			x = 50,
			y = 0,
		}
	}
})

WelcomeHome.frames = {}


function WelcomeHome:OnEnable()
	-- called when the addon is enabled
	self:Print("I'm Sexy!")
	self:RegisterEvent("ZONE_CHANGED")
	
	self:TestBar()
	self:TestBar2()
	
	self:Print("X: " .. self.db.profile.frames.anchorFrameLoc.x)
	self:Print("Y: " .. self.db.profile.frames.anchorFrameLoc.y)
end

function WelcomeHome:ZONE_CHANGED()
	if self.db.profile.showInChat then
		self:Print(self.db.profile.message)
	end
	
	if self.db.profile.showOnScreen then
		UIErrorsFrame:AddMessage(self.db.profile.message, 1, 0, 1, 5)
	end
end

function WelcomeHome:TestBar()
	
	-- create an anchor frame to anchor candy bars to
	local f = CreateFrame("Frame", nil, UIParent)
	f:SetWidth(100)
	f:SetHeight(25)
	f:SetPoint("TOPLEFT", "UIParent", self.db.profile.frames.anchorFrameLoc.x, self.db.profile.frames.anchorFrameLoc.y)
	f:SetBackdrop({
		bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 2, edgeSize = 8,
		insets = { left = 2, right = 2, top = 2, bottom = 2 }
		})
	f:RegisterForDrag("LeftButton");
	f:EnableMouse(true)
	f:SetMovable(true)
	f:SetScript("OnDragStart", function()
		this:StartMoving();
		this.isMoving = true;

		-- add on update		
		WelcomeHome:Print("adding onUpdate widget event")	
		this:SetScript("OnUpdate", function() self:UpdateAnchorFrame() end)
	end)
	
	f:SetScript("OnDragStop", function()
		this:StopMovingOrSizing();
		this.isMoving = false;
		-- remove on update
		WelcomeHome:Print("removing onUpdate widget event")
		this:SetScript("OnUpdate", nil)
		
		local _,_,_,x,y = self.frames.anchorFrame.frame:GetPoint()
		self.db.profile.frames.anchorFrameLoc.x = math.floor(x)
		self.db.profile.frames.anchorFrameLoc.y = math.floor(y)
		end)
	
	
	-- create some text to display the location of the anchor frame
	local t = f:CreateFontString(nil, "ARTWORK", "GameFontNormal")
	t:SetPoint ("LEFT", f, "LEFT", 8, 0)
	t:SetText(self.db.profile.frames.anchorFrameLoc.x ..", ".. self.db.profile.frames.anchorFrameLoc.y)
	
	self.frames.anchorFrame = {
		frame = f,
		text = t,
		cachedX = self.db.profile.frames.anchorFrameLoc.x,
		cachedY = self.db.profile.frames.anchorFrameLoc.y,
	}
	
	-- register some test bars

	self:RegisterCandyBarGroup("TestBarGroup")
	self:SetCandyBarGroupPoint("TestBarGroup", "TOPLEFT", self.frames.anchorFrame.frame, "BOTTOMLEFT", -16, 0)
	self:SetCandyBarGroupGrowth("TestBarGroup", false)
	for x = 1, 10, 1 do
		local id = "TestBar_"..x
		self:RegisterCandyBar(id, math.random(5,30), "Test"..x,
			"Interface\\Addons\\Food\\Banana.tga",
			"green", "Yellow", "Red")
			
		self:SetCandyBarScale(id, 1)
		self:RegisterCandyBarWithGroup(id, "TestBarGroup")
		self:StartCandyBar(id, true)
		--self:PauseCandyBar(id, true)
		
		-- test clickable
		self:SetCandyBarOnClick(id, function() self:Print("candy bar "..x.." clicked") end)
	end
end

function WelcomeHome:TestBar2()
	
	-- create the main frame
	local f = CreateFrame("Frame", nil, UIParent)
	f:SetWidth(300)
	f:SetHeight(300)
	f:SetPoint("TOPLEFT", "UIParent", 400, -300)
	f:SetBackdrop({
		bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
		edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
		tile = true, tileSize = 2, edgeSize = 8,
		insets = { left = 2, right = 2, top = 2, bottom = 2 }
		})
	f:SetBackdropColor(0,0,0,0.50)
	
	self.frames.dragFrame = {frame = f, dragBars = {}}
	
		-- create some option frames
	for x = 1, 10, 1 do
		local f = CreateFrame("Frame", nil, self.frames.dragFrame.frame)
		f:SetWidth(275)
		f:SetHeight(25)
		f:SetPoint("TOP", self.frames.dragFrame.frame, "TOP", 0, (-25 * x))
		f:SetBackdrop({
			bgFile = "Interface\\TutorialFrame\\TutorialFrameBackground",
			edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
			tile = true, tileSize = 2, edgeSize = 16,
			insets = { left = 2, right = 2, top = 2, bottom = 2 }
			})
		f:SetBackdropColor(0,0,0,0.75)
		
		f:EnableMouse(true)
		--f:SetScript("OnClick", function()
		--	self:Print("you have clicked dragable bar # " .. x)
		--end)
		
		local t = f:CreateFontString(nil, "ARTWORK", "GameFontNormal")
		t:SetPoint ("LEFT", f, "LEFT", 8, 0)
		t:SetText("Bar "..x)
		
		self.frames.dragFrame.dragBars[x] = {
			frame = f,
			text = t,
		}
	end
	
	
	
end

function WelcomeHome:UpdateAnchorFrame()
	local point, relativeTo, relativePoint, rawX, rawY = self.frames.anchorFrame.frame:GetPoint()
	local x = math.floor(rawX)
	local y = math.floor(rawY)
	if (x ~= self.frames.anchorFrame.cachedX) or (y ~= self.frames.anchorFrame.cachedY) then
		-- self:Print("X: " ..x.. "Y: " ..y)
		self.frames.anchorFrame.cachedX = x
		self.frames.anchorFrame.cachedY = y
		self.frames.anchorFrame.text:SetText(x ..", ".. y)
	end
end

-- --------------------
-- options functions
-- --------------------
function WelcomeHome:GetMessage()
	return self.db.profile.message
end

function WelcomeHome:SetMessage(newValue)
	self.db.profile.message = newValue
end

function WelcomeHome:IsShowInChat()
	return self.db.profile.showInChat
end

function WelcomeHome:ToggleShowInChat()
	self.db.profile.showInChat = not self.db.profile.showInChat
end

function WelcomeHome:IsShowOnScreen()
	return self.db.profile.showOnScreen
end

function WelcomeHome:ToggleShowOnScreen()
	self.db.profile.showOnScreen = not self.db.profile.showOnScreen
end